#ifndef OSAL_IMPL_RTTHREAD_H
#define OSAL_IMPL_RTTHREAD_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdbool.h>
#include "device.h"

#include <rtthread.h>

typedef rt_sem_t                    OSAL_SEM_HANDLE_TYPE;
typedef rt_mutex_t                  OSAL_MUTEX_HANDLE_TYPE;
typedef rt_uint32_t                 OSAL_CRITSECT_DATA_TYPE;
#define OSAL_WAIT_FOREVER           (uint16_t) (-1)

#define OSAL_SEM_DECLARE(semID)         OSAL_SEM_HANDLE_TYPE    semID
#define OSAL_MUTEX_DECLARE(mutexID)     OSAL_MUTEX_HANDLE_TYPE  mutexID

typedef enum OSAL_SEM_TYPE {
    OSAL_SEM_TYPE_BINARY,
    OSAL_SEM_TYPE_COUNTING
} OSAL_SEM_TYPE;

typedef enum OSAL_CRIT_TYPE {
    OSAL_CRIT_TYPE_LOW,
    OSAL_CRIT_TYPE_HIGH
} OSAL_CRIT_TYPE;

typedef enum OSAL_RESULT {
    OSAL_RESULT_NOT_IMPLEMENTED = -1,
    OSAL_RESULT_FALSE = 0,
    OSAL_RESULT_TRUE = 1
} OSAL_RESULT;

// *****************************************************************************
// *****************************************************************************
// Section: Section: Interface Routines Group Declarations
// *****************************************************************************
// *****************************************************************************
OSAL_RESULT OSAL_SEM_Create (OSAL_SEM_HANDLE_TYPE *semID, OSAL_SEM_TYPE type, uint8_t maxCount, uint8_t initialCount);
OSAL_RESULT OSAL_SEM_Delete (OSAL_SEM_HANDLE_TYPE *semID);
OSAL_RESULT OSAL_SEM_Pend (OSAL_SEM_HANDLE_TYPE *semID, uint16_t waitMS);
OSAL_RESULT OSAL_SEM_Post (OSAL_SEM_HANDLE_TYPE *semID);
OSAL_RESULT OSAL_SEM_PostISR (OSAL_SEM_HANDLE_TYPE *semID);
uint8_t OSAL_SEM_GetCount (OSAL_SEM_HANDLE_TYPE *semID);

OSAL_CRITSECT_DATA_TYPE OSAL_CRIT_Enter (OSAL_CRIT_TYPE severity);
void OSAL_CRIT_Leave (OSAL_CRIT_TYPE severity, OSAL_CRITSECT_DATA_TYPE status);

OSAL_RESULT OSAL_MUTEX_Create (OSAL_MUTEX_HANDLE_TYPE *mutexID);
OSAL_RESULT OSAL_MUTEX_Delete (OSAL_MUTEX_HANDLE_TYPE *mutexID);
OSAL_RESULT OSAL_MUTEX_Lock (OSAL_MUTEX_HANDLE_TYPE *mutexID, uint16_t waitMS);
OSAL_RESULT OSAL_MUTEX_Unlock (OSAL_MUTEX_HANDLE_TYPE *mutexID);

void *OSAL_Malloc (size_t size);
void OSAL_Free (void *pData);

OSAL_RESULT OSAL_Initialize (void);

// Miscellaneous functions
// *****************************************************************************
/* Function: void* OSAL_Malloc(size_t size)
 */
#define OSAL_Malloc(size)                               (rt_malloc(size))

// *****************************************************************************
/* Function: void OSAL_Free(void* pData)
 */
#define OSAL_Free(pData)                                (rt_free(pData))

// *****************************************************************************
/* Function: const char* OSAL_Name()
 */
__STATIC_INLINE __attribute__ ((always_inline)) const char *OSAL_Name (void)
{
    return ((const char *) "RTT");
}

#ifdef __cplusplus
}
#endif

#endif // OSAL_IMPL_RTTHREAD_H

/*******************************************************************************
 End of File
 */
